package com.umeng.community.example.Utils;

import android.content.Context;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.qianxx.base.IConstants;
import com.qianxx.base.Urls;
import com.qianxx.base.request.Config;
import com.qianxx.base.request.RM;
import com.qianxx.base.request.RequestBean;
import com.qianxx.base.request.RequestCallback;
import com.qianxx.base.request.RequestUtil;
import com.qianxx.base.utils.SPUtil;
import com.qianxx.base.utils.StringUtil;
import com.qianxx.base.utils.TimeUtil;
import com.umeng.comm.core.beans.CommConfig;
import com.umeng.comm.core.beans.CommUser;
import com.umeng.comm.core.beans.Source;
import com.umeng.comm.core.impl.CommunitySDKImpl;
import com.umeng.comm.core.login.LoginListener;
import com.umeng.comm.ui.imagepicker.activities.SettingActivity;
import com.umeng.community.example.bean.UserBean;

import java.util.HashMap;

/**
 * Created by zhangbingyong on 16/5/19.
 */
public class ThirdLoginRequestUtils implements RequestCallback {
    private ThirdLoginRequestUtils() {
    }

    private static ThirdLoginRequestUtils requestUtils;
    private static Context mContext;
    protected RequestQueue mRequest; // 请求队列对象


    public static ThirdLoginRequestUtils getInstance(Context context) {

        if (requestUtils == null) {
            synchronized (ThirdLoginRequestUtils.class) {
                if (requestUtils == null) {
                    requestUtils = new ThirdLoginRequestUtils();

                }
            }
        }
        mContext = context;

        return requestUtils;
    }

    /**
     * 第三方登录后，登录服务器
     */
    public void ThirdLogin(CommUser userInfo) {

        CommConfig.getConfig().loginedUser = userInfo;
        HashMap<String, String> map = new HashMap<>();
        if (userInfo.source == Source.QQ) {
//            SPUtil.getInstance().setType("QQ");
            map.put("type", "qq");
        } else if (userInfo.source == Source.WEIXIN) {
//            SPUtil.getInstance().setType("wx");
            map.put("type", "wx");
        } else if (userInfo.source == Source.SINA) {
//            SPUtil.getInstance().setType("wb");
            map.put("type", "wb");
        }
        map.put("openId", userInfo.sourceUid);
        map.put("nickName", userInfo.name);
        map.put("headImgUrl", userInfo.iconUrl);
        requestDataWithoutLoading(IConstants.REQUEST, Urls.THIRDLOGIN_LIST, RM.POST, UserBean.class, map);
    }

    public void ThirdLogin(CommUser userInfo, HideLoading hideLoading) {

        this.hideLoading = hideLoading;
        CommConfig.getConfig().loginedUser = userInfo;
        HashMap<String, String> map = new HashMap<>();
        if (userInfo.source == Source.QQ) {
//            SPUtil.getInstance().setType("QQ");
            map.put("type", "qq");
        } else if (userInfo.source == Source.WEIXIN) {
//            SPUtil.getInstance().setType("wx");
            map.put("type", "wx");
        } else if (userInfo.source == Source.SINA) {
//            SPUtil.getInstance().setType("wb");
            map.put("type", "wb");
        }
        map.put("openId", userInfo.sourceUid);
        map.put("nickName", userInfo.name);
        map.put("headImgUrl", userInfo.iconUrl);
        requestDataWithoutLoading(IConstants.REQUEST, Urls.THIRDLOGIN_LIST, RM.POST, UserBean.class, map);
    }

    private HideLoading hideLoading;

    public HideLoading getHideLoading() {
        return hideLoading;
    }

    public void setHideLoading(HideLoading hideLoading) {
        this.hideLoading = hideLoading;
    }

    public interface HideLoading {
        public void setHideLoading();
    }

    /**
     * 不带loading指示的数据请求
     */
    private void requestDataWithoutLoading(String requestTag, String url,
                                           RM method, Class<? extends RequestBean> bean,
                                           HashMap<String, String> params) {
        if (mRequest == null) {
            mRequest = Volley.newRequestQueue(mContext);
        }
        RequestUtil.requestData(mRequest, requestTag, url, method, bean,
                params, this, new Config().setShowLoading(false));
    }

    @Override
    public void requestSuccess(RequestBean baseBean, Config config) {
        if (hideLoading != null) {
            hideLoading.setHideLoading();
        }
        UserBean userBean = (UserBean) baseBean;
        SPUtil.getInstance().setToken(userBean.getData().getToken());
        SPUtil.getInstance().setLongAge(TimeUtil.getStrTime5(userBean.getData().getMyInfo().getBirthday()));
        SPUtil.getInstance().setAge(StringUtil.age(TimeUtil.getStrTime5(userBean.getData().getMyInfo().getBirthday())) + "");
        if (userBean.getData().getMyInfo().getStatus().equals("1")) {
        } else {
            SettingActivity.actionStart(mContext, false);
        }
    }

    @Override
    public void requestFail(RequestBean bean, Config config) {
        if (bean == null) {
            CommunitySDKImpl.getInstance().logout(mContext, new LoginListener() {
                @Override
                public void onStart() {

                }

                @Override
                public void onComplete(int stCode, CommUser userInfo) {

                }
            });
        }

    }

    @Override
    public void requestTokenInvalid(String requestTag, Config config) {

    }
}
